//
//  YAYIDiscountView.m
//  YAYIDoctor
//
//  Created by hxp on 16/5/6.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIDiscountView.h"

@implementation YAYIDiscountView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataAry = [NSMutableArray array];
        [self createDataSource];
        [self createViews];
        [self createPickerView];
    }
    return self;
}
-(void)createViews{
    UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cButton.userInteractionEnabled = YES;
    [cButton setTitle:@"取消" forState:UIControlStateNormal];
    cButton.tag =0;
    cButton.titleLabel.font = [UIFont systemFontOfSize:font14];
    [cButton  addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cButton setTitleColor:YAYIColor(83, 165, 243) forState:UIControlStateNormal];
    [self addSubview:cButton];
    [cButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(25));
        make.top.equalTo(@(18));
        make.size.mas_equalTo(CGSizeMake(60, 20));
    }];
    UILabel *title = [UILabel new];
    title.text = @"本单折扣";
    title.font = [UIFont systemFontOfSize:font14];
    title.textAlignment = NSTextAlignmentCenter;
    [self addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(cButton.mas_top);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(120, 20));
    }];
    
    UIButton *sButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sButton.tag =1;
    [sButton setTitle:@"完成" forState:UIControlStateNormal];
    sButton.titleLabel.font = [UIFont systemFontOfSize:font14];
    [sButton  addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [sButton setTitleColor:YAYIColor(83, 165, 243) forState:UIControlStateNormal];
    [self addSubview:sButton];
    [sButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).with.offset(-25);
        make.top.equalTo(@(18));
        make.size.mas_equalTo(CGSizeMake(60, 20));
    }];
}
-(void)buttonAction:(UIButton *)sender
{
    
    [self removeFromSuperview];
    [self.pickerView  removeFromSuperview];
     self.pickerView = nil;
    if (sender.tag == 1) {
        if (_delegate &&[_delegate respondsToSelector:@selector(discount:)]) {
            [_delegate discount:self.discount];
        }
    }else{
        if (_delegate &&[_delegate respondsToSelector:@selector(discount:)]) {
            [_delegate discount:@""];
        }
    }
}
-(void)createPickerView{
    if (self.pickerView == nil) {
        self.pickerView = [UIPickerView new];
        self.pickerView.frame = CGRectMake(0, 40, self.frame.size.width, 180);
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
        [self addSubview:self.pickerView];
    }
    [self.pickerView reloadAllComponents];
}
-(void)createDataSource
{
    for (int i = 100; i > 0; i--) {
        [self.dataAry addObject:@(i)];
    }
    [self.pickerView reloadAllComponents];
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 30;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataAry.count;

}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"%@",self.dataAry[row]];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.discount = [NSString stringWithFormat:@"%@",self.dataAry[row]];
}
@end
